package sample.controls;

import org.dreamwork.jasmine2.annotation.ParseChildren;
import org.dreamwork.jasmine2.annotation.TemplateSupport;
import org.dreamwork.jasmine2.annotation.WebControlTemplate;
import org.dreamwork.jasmine2.template.ITemplate;
import org.dreamwork.jasmine2.template.ITemplateControl;
import org.dreamwork.jasmine2.web.controls.Literal;
import org.dreamwork.jasmine2.web.controls.WebContainer;
import org.dreamwork.jasmine2.web.html.GenericHtmlControl;
import sample.syntaxparser.JavaParser;
import sample.syntaxparser.Role;

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 2010-9-15
 * Time: 17:00:48
 */
@TemplateSupport
@ParseChildren
@WebControlTemplate (SyntaxRenderControlTemplate.class)
public class SyntaxRenderControl extends WebContainer implements ITemplateControl {
    protected SyntaxRenderControlTemplate template;
    private String language = "java";
    private String url = null;
    private String body;
    private Map<String, String> css = new HashMap<String, String> ();
    private List<Role> roles;

    public SyntaxRenderControl () {
        this.htmlTagName = "div";
    }

    public String getLanguage () {
        return language;
    }

    public void setLanguage (String language) {
        this.language = language;
    }

    public String getUrl () {
        return url;
    }

    public void setUrl (String url) {
        this.url = url;
    }

    public void setTemplate (ITemplate template) {
        this.template = (SyntaxRenderControlTemplate) template;
    }

    public void applyTemplate () {
        if (template == null) return;

        language = template.getLanguage ();
        body = template.getBody ();
        url = template.getUrl () == null ? this.url : template.getUrl ();
        roles = template.getRoles ();
        for (Role role : roles) {
            if (role.getCssClass () != null) continue;
            String cssName = "in-" + role.getName ();
            if (role.getStyle () != null) {
                css.put (cssName, role.getStyle ());
            } else {
                StringBuilder builder = new StringBuilder ();
                Map<String, String> a = role.getAttributes ();
                for (String key : a.keySet ()) {
                    if (builder.length () > 0) builder.append (';');
                    builder.append (key).append (':').append (a.get (key));
                }
                css.put (cssName, builder.toString ());
            }
            role.setCssClass (cssName);
        }
    }

    @Override
    public void createChildControls () {
        try {
        childControls.clear ();
        if (css.size () > 0) {
            StringBuilder builder = new StringBuilder ("<style type='text/css'>");
            for (String key : css.keySet ()) {
                builder.append ("\r\n");
                builder.append ('.').append (key).append (" {").append (css.get (key)).append ('}');
            }
            builder.append ("</style>");
            addControl (new Literal (builder));
        }
        GenericHtmlControl pre = new GenericHtmlControl ("pre");
/*
        pre.setStyle ("font-size", "12px");
        pre.setStyle ("font-family", "monospace \"Courier New\"");
*/
        JavaParser parser = null;
        if (body != null) parser = new JavaParser (body);
        else if (url != null) {
            URL u = new URL (url);
            Reader reader = new InputStreamReader (u.openStream (), "utf-8");
            parser = new JavaParser (reader);
        }
        if (parser == null) return;
        parser.setRoles (roles);
        pre.addControl (new Literal (parser.parse ()));
        addControl (pre);
            childControlCreated = true;
        } catch (Exception ex) {
            throw new RuntimeException (ex);
        }
    }
}